package eerbaar.shared.persist.internal.lib;

import eerbaar.level.IHint;
import eerbaar.level.LevelFactory;
import eerbaar.shared.exceptions.GameRuntimeException;
import eerbaar.shared.persist.IPersister;
import eerbaar.shared.persist.ISaver;
import eerbaar.shared.tree.IAttributes;
import eerbaar.shared.tree.INode;

public class HintPersister implements IPersister<IHint> {

    public static final String NAME = "hint";

    private IHint target;

    public HintPersister() {
        //
    }

    public HintPersister(IHint wrapper) {
        this.target = wrapper;
    }

    public HintPersister(INode node) throws GameRuntimeException {
        load(node);
    }

    @Override
    public Class<IHint> getRegistertedInterface() {
        return IHint.class;
    }

    @Override
    public ISaver<?>[] getSavableChildren() {

        return new ISaver<?>[] {};
    }

    @Override
    public String getSavableName() {

        return NAME;
    }

    @Override
    public IHint getTarget() {
        return this.target;
    }

    @Override
    public boolean isAssignableFrom(ISaver<?> savable) {
        return false;
    }

    @Override
    public IHint load(INode node) {
        assert (node != null);

        IHint result = LevelFactory.createHint();

        IAttributes attributes = node.getAttributes();
        String message = attributes.get(String.class, "message");
        result.setMessage(message);

        return result;
    }

    @Override
    public void saveAttributes(INode node) {

        IAttributes a = node.getAttributes();
        a.set(String.class, "message", this.target.getMessage());
    }

    @Override
    public void setTarget(IHint obj) {
        this.target = obj;
    }

}
